<script setup lang="ts">
import { ConsultType } from '@/enum'
import ConsultItem from './ConsultItem.vue'
import { ref } from 'vue'
import { getConsultOrderList } from '@/services/consult'
import type { ConsultOrderItem, ConsultOrderListParams } from '@/types/consult'

const props = defineProps<{
  type: ConsultType
}>()

// 1. 请求获取列表数据
const list = ref<ConsultOrderItem[]>([])
const loading = ref(false)
const finished = ref(false)
const params = ref<ConsultOrderListParams>({
  current: 1,
  pageSize: 5,
  type: props.type
})
const onLoad = async () => {
  // 1.1 调用接口
  const res = await getConsultOrderList(params.value)
  list.value.push(...res.data.rows)

  // 1.2 判断数据是否加载完毕
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    params.value.current++
  }

  // 1.3 修改加载状态
  loading.value = false
}

// 2. 根据订单的id去删除订单
const deleteOrder = (id: string) => {
  const index = list.value.findIndex((obj) => obj.id === id)
  list.value.splice(index, 1)
}
</script>

<template>
  <div class="consult-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <consult-item
        :item="item"
        v-for="item in list"
        :key="item.id"
        @delete-order="deleteOrder"
      />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.consult-list {
  padding: 10px 15px;
}
</style>
